<?php
/**
 * Created by PhpStorm.
 * User: lelouchms
 * Date: 2019/3/28
 * Time: 16:08
 */

namespace app\backend\controller\admin\v1;

use app\backend\controller\BackendController;
use app\common\constants\DBConstant;
use app\common\exception\CustomerException;
use app\common\exception\ValidateException;
use app\common\util\CommonUtil;

class Entrust extends Base
{

    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\common\model\Entrust();
    }

    public function index()
    {
        list($where, $sort, $order, $offset, $limit, $infinite) = $this->buildParams();
        $count = $this->model->with(["region"])->where($where)->order($sort, $order)->count();
        $query = $this->model
            ->allowField(true)
            ->where($where)
            ->with(["region"])
            ->order($sort, $order);
        if ($infinite !== true) {
            $query->limit($offset, $limit);
        }
        if (!empty($this->expectFields)) $query->field($this->dataTablePrefix($this->expectFields), true);
        $list = $query->select();
        $returnData = [
            "count" => $count,
            "list" => $list
        ];
        return $returnData;
    }

    public function getStatusList()
    {
        return model("entrust")->getStatusList();
    }

    public function update()
    {
        $requestData = $this->request->only(['status', 'id', 'area_id', "house_name", "username", "phone", "remark"]);
        $validate = new \app\common\validate\Entrust();
        $flag = $validate->scene("update")->check($requestData);
        if ($flag === false) {
            throw new ValidateException($validate->getError());
        }
        $count = $this->model->where("id", $requestData["id"])->update($requestData);
        if ($count > 1) throw new CustomerException("编辑失败");
        $this->_removeCache($requestData);
    }


}